package com.xinhe.web.controller;

import com.querydsl.core.QueryResults;
import com.xinhe.web.annotation.WebUsed;
import com.xinhe.web.common.Result;
import com.xinhe.web.entity.BankCard;
import com.xinhe.web.request.ServiceCenterQuery;
import com.xinhe.web.service.AliyunService;
import com.xinhe.web.service.BankCardService;
import com.xinhe.web.service.ServiceCenterService;
import com.xinhe.web.util.ValidatorUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Api(value = "银行卡信息", tags = {"银行卡信息"})
@RestController
@RequestMapping(value = {"/bankCard"}, produces = {"application/json;charset=UTF-8"})
public class BankCardController {

    @Autowired
    private BankCardService bankCardService;
    @WebUsed
    @ApiOperation(httpMethod = "GET", value = "查询用户默认银行卡")
    @RequestMapping({"/findBankByUserAndIsDefault"})
    public Result findBankByUserAndIsDefault(Long userId) {
        return bankCardService.findBankByUserAndIsDefault(userId);
    }

    @ApiOperation(httpMethod = "GET", value = "分页查询")
    @RequestMapping({"/pageBankCard"})
    QueryResults<BankCard> pageBankCard(String bankType,String code,String name,
                                          String bankName,Integer isDefault,String remark,int pageNum, int pageSize) {
        return this.bankCardService.pageBankCard(bankType,code,name,bankName,isDefault,remark, pageNum,  pageSize);
    }

    @ApiOperation(httpMethod = "GET", value = "分页查询(用户)")
    @RequestMapping({"/pageBankCardByUser"})
    QueryResults<BankCard> pageBankCardByUser(Long userId, String bankType, String code, String name,
                                              String bankName, Integer isDefault, String remark,int pageNum, int pageSize) {
        return this.bankCardService.pageBankCardByUser(userId,bankType,code,name,bankName,isDefault,remark, pageNum,  pageSize);
    }


    @ApiOperation(httpMethod = "POST", value = "保存")
    @RequestMapping({"/saveBankCard"})
    public Result saveBankCard(Long id,String bankType,String code,String name,String bankName,
                                             Integer isDefault,String remark) {
        if (isDefault == null) {
            isDefault = 0;
        }
        return this.bankCardService.saveBankCard(id,bankType, code, name, bankName,isDefault, remark);
    }

    @ApiOperation(httpMethod = "GET", value = "删除")
    @ApiImplicitParam(name = "bankCardId", value = "标识", required = true)
    @RequestMapping({"/deleteBankCard"})
    public Result deleteBankCard(Long bankCardId) {
        this.bankCardService.deleteBankcard(bankCardId);
        return Result.ok();
    }

    @ApiOperation(httpMethod = "GET", value = "查询详情")
    @ApiImplicitParam(name = "bankCardId", value = "标识", required = true)
    @RequestMapping({"/detailBankCard"})
    public Result<BankCard> detailBankCard(Long bankCardId) {
        return Result.ok(this.bankCardService.findBankByBankCardId(bankCardId));
    }

    @ApiOperation(httpMethod = "GET", value = "查询自己默认银行卡")
    @RequestMapping({"/findBankByIsDefault"})
    public Result<BankCard> findBankByIsDefault() {
        BankCard bankcard = this.bankCardService.findBankByIsDefault();
        return Result.ok(bankcard);
    }

    @ApiOperation(httpMethod = "GET", value = "查询用户银行卡")
    @RequestMapping({"/findBankByUser"})
    public Result findBankByUser(Long userId) {
        List<BankCard> bankcards = this.bankCardService.findBankByUser(userId);
        return Result.ok(bankcards);
    }
    @ApiOperation(httpMethod = "POST", value = "设置默认银行卡")
    @RequestMapping({"/setDefault"})
    public Result setDefault(Long userId,Long cardId) {
        this.bankCardService.setDefault(userId,cardId);
        return Result.ok();
    }
    @ApiOperation(httpMethod = "POST", value = "查询详情")
    @RequestMapping({"/findBankByBankCardId"})
    public Result<BankCard> findBankByBankCardId(Long bankcardId) {
        BankCard bankcard = this.bankCardService.findBankByBankCardId(bankcardId);
        return Result.ok(bankcard);
    }
}
